package com.itzjc.service.impl;

import com.itzjc.dao2.DataMapper2;
import com.itzjc.domain.entity.*;
import com.itzjc.domain.pojo.AllCurrentData;
import com.itzjc.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 由于都是查询，所以查出的数据遗漏的没有影响，所以就没用启动事务
 *
 * @className: DataServiceImpl
 * @date: 2021-06-28 20:56
 **/
@Service
public class DataServiceImpl implements DataService {
    @Autowired(required = false)
    private DataMapper2 dataMapper2;


    @Override
    public List<TableData> getAllInitDataByToken() {
        //从关系表中获取除值以外的其他信息
        List<AllCurrentData> allCurrentData = dataMapper2.getAllInitDataExceptValue();
        //从数据表中获取最新的数据信息
        List<Double> value = dataMapper2.getAllInitValue();
        for (int i = 0; i < allCurrentData.size(); i++) {
            allCurrentData.get(i).setValue(value.get(i));
        }
        List<TableData> tableDataList = new ArrayList<>();

        for (AllCurrentData allCurrentDatum : allCurrentData) {
            TableData tableData = new TableData();
            tableData.setPlane("测区" + allCurrentDatum.getLocation());
            tableData.setSectionID("测站" + allCurrentDatum.getSectionID());
            tableData.setSid(allCurrentDatum.getSID());
            switch (allCurrentDatum.getStype()) {
                case 1:
                    tableData.setStype("应力");
                    break;
                case 2:
                    tableData.setStype("位移");
                    break;
                case 3:
                    tableData.setStype("挠度");
                    break;
                case 4:
                    tableData.setStype("温度");
                    break;
                case 5:
                    tableData.setStype("应变");
                    break;
                default:
                    tableData.setStype("其他");
            }
            tableData.setUnit(allCurrentDatum.getUnit());
            tableData.setValue(allCurrentDatum.getValue());
            tableData.setRatio(1.0);
            tableData.setPosX(allCurrentDatum.getPosX());
            tableData.setPosY(allCurrentDatum.getPosY());
            tableData.setPosZ(allCurrentDatum.getPosZ());
            //保留小数，在进行之前做非空判断
            if (null!=allCurrentDatum.getLmax()&&null!=allCurrentDatum.getLmin()) {
                double  f   =   allCurrentDatum.getLmax();
                double  f1   =   allCurrentDatum.getLmin();
                BigDecimal b   =   new   BigDecimal(f);
                BigDecimal b1   =   new   BigDecimal(f1);
                double   f2   =   b.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
                double   f3   =   b1.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
                tableData.setLmin(f2);
                tableData.setLmax(f3);

            }

            tableDataList.add(tableData);
        }
        return tableDataList;
    }

    @Override
    public TableBaseInfo getBaseInfoByToken() {
        BigInteger dataTime = dataMapper2.getBaseTime();
        Integer planesNumber = dataMapper2.getBasePlanesNumber();
        Integer sitesNumber = dataMapper2.getBaseSitesNumber();
        Integer pointsNumber = dataMapper2.getBasePointsNumber();

        TableBaseInfo tableBaseInfo = new TableBaseInfo();
        //转换数据格式
        long longValue = dataTime.longValue();
        //倒推标准时间所需要减去的时间
        long baseLongValue = 404467200000L;
        Date date = new Date(Long.valueOf(longValue - baseLongValue));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        tableBaseInfo.setTime(time);
        tableBaseInfo.setPlanesNumber(planesNumber);
        tableBaseInfo.setSitesNumber(sitesNumber);
        tableBaseInfo.setPointsNumber(pointsNumber);
        return tableBaseInfo;
    }

    @Override
    public TitleInfo gettableTitleByToke() {
        List<Integer> planes = dataMapper2.gettableTitleOfPlanes();
        List<Integer> sites = dataMapper2.gettableTitleOfSites();
        List<Integer> types = dataMapper2.gettableTitleOfTypes();
        List<TableTileOfPlanes> tableTileOfPlanes = new ArrayList<>();
        List<TableTileOfSites> tableTileOfSites = new ArrayList<>();
        List<TableTileOfTypes> tableTileOfTypes = new ArrayList<>();


        for (Integer plane : planes) {
            TableTileOfPlanes tableTileOfPlanes1 = new TableTileOfPlanes();
            tableTileOfPlanes1.setText("测区" + plane);
            tableTileOfPlanes1.setValue("测区" + plane);
            tableTileOfPlanes.add(tableTileOfPlanes1);
        }
        for (Integer site : sites) {
            TableTileOfSites tableTileOfSites1 = new TableTileOfSites();
            tableTileOfSites1.setText("测站" + site);
            tableTileOfSites1.setValue("测站" + site);
            tableTileOfSites.add(tableTileOfSites1);
        }
        for (Integer type : types) {
            TableTileOfTypes tableTileOfTypes1 = new TableTileOfTypes();
            switch (type) {
                case 1:
                    tableTileOfTypes1.setText("应力");
                    tableTileOfTypes1.setValue("应力");
                    break;
                case 2:
                    tableTileOfTypes1.setText("位移");
                    tableTileOfTypes1.setValue("位移");
                    break;
                case 3:
                    tableTileOfTypes1.setText("挠度");
                    tableTileOfTypes1.setValue("挠度");
                    break;
                case 4:
                    tableTileOfTypes1.setText("温度");
                    tableTileOfTypes1.setValue("温度");
                    break;
                case 5:
                    tableTileOfTypes1.setText("应变");
                    tableTileOfTypes1.setValue("应变");
                    break;
                default:
                    tableTileOfTypes1.setText("其他");
                    tableTileOfTypes1.setValue("其他");
            }
            tableTileOfTypes.add(tableTileOfTypes1);
        }

        TitleInfo titleInfo = new TitleInfo();
        titleInfo.setPlanes(tableTileOfPlanes);
        titleInfo.setSites(tableTileOfSites);
        titleInfo.setTypes(tableTileOfTypes);
        return titleInfo;
    }
}
